#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10, M = 2 * N;
int h[N], ne[M], e[M], idx;
int f[N][22];
int dep[N];
int sz[N];
int son_1[N];
int n;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u, int fa)
{
	sz[u] = 1;
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == fa)continue;
		dep[j] = dep[u] + 1;
		// 		f[j][0] = u;
		// 		for (int k = 1; (1 << k) <= dep[j]; k++) {
		// 			f[j][k] = f[f[j][k - 1]][k - 1];
		// 		}
		dfs(j, u);
		sz[u] += sz[j];
		if (sz[j] == 1)son_1[u]++;
	}
}
void dfs1(int u, int fa)
{
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == fa)continue;
		dfs1(j, u);
		son_1[u] += son_1[j];
	}
}

int lca(int a, int b)
{
	if (dep[a] < dep[b])swap(a, b);
	int d = dep[a] - dep[b];
	for (int i = 0; d; i++) {
		if (d & 1)a = f[a][i];
		d /= 2;
	}
	if (a == b)return a;
	for (int i = 21; i >= 0; i--) {
		if (f[a][i] != f[b][i]) {
			a = f[a][i];
			b = f[b][i];
		}
	}
	return f[a][0];
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		//memset(h, -1, sizeof(h));
		idx = 0;
		// 		for (int i = 1; i <= n; i++) {
		// 			for (int j = 0; j < 21; j++) {
		// 				f[i][j] = 0;
		// 			}
		// 		}
		for (int i = 1; i <= n; i++) {
			h[i] = -1;
			sz[i] = 0;
			son_1[i] = 0;
		}
		for (int i = 1; i <= n * 2; i++) {
			e[i] = 0;
			ne[i] = 0;
		}
		int q;
		for (int i = 0; i < n - 1; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			add(x, y);
			add(y, x);
		}
		scanf("%d", &q);
		LL cnt = 0;
		dfs(1, 0);
		dfs1(1, 0);
		for (int i = 1; i <= n; i++) {
			if (sz[i] == 1)cnt++;
		}
		//cout<<"cnt=="<<cnt<<endl;
		/*for (int i = 1; i <= n; i++) {
			cout << son_1[i] << endl;
		}*/
		while (q--) {
			int x, y;
			scanf("%d%d", &x, &y);
			LL ans = 0;
			//int k = lca(x, y);
			//cout<<"k=="<<k<<endl;
			bool flag = 0;
			if (sz[x] == 1 && sz[y] >= 2 || sz[y] == 1 && sz[x] >= 2) {
				if (sz[y] >= 2)ans = son_1[y];
				if (sz[x] >= 2)ans = son_1[x];
			}
			else if (sz[x] == 1 && sz[y] == 1) {
				ans = 1;
			}
			else if (sz[x] >= 2 && sz[y] >= 2) {
				if (cnt == 1)ans = 1;
				else {
					//	if (dep[x] >= dep[k] && dep[y] >= dep[k])
					ans = 1ll * (son_1[x]) * (son_1[y]);
				}
			}
			printf("%lld\n", ans);
		}
	}
	return 0;
}